package com.stn.zjx.givingtreasure.activity.search;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.MainFragmentAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.fragment.search.ShopSearchFragment;
import com.stn.zjx.givingtreasure.fragment.search.StoreSearchFragment;
import com.stn.zjx.givingtreasure.utils.SoftInputUtils;
import com.stn.zjx.givingtreasure.widget.NoScrollViewPager;
import com.vondear.rxtools.RxActivityTool;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindArray;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class StoreShopSearchResultActivity extends BaseActivity implements TabLayout.BaseOnTabSelectedListener {

    @BindArray(value = R.array.store_shop)
    String[] store_shop_ar;
    @BindView(R.id.viewpager)
    NoScrollViewPager viewpager;
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.search_tv_bt)
    TextView searchTvBt;
    @BindView(R.id.robot_open_bt)
    RelativeLayout robotOpenBt;
    @BindView(R.id.clear_search_content_bt)
    RelativeLayout clearSearchContentBt;
    @BindView(R.id.search_store_edit)
    EditText searchStoreEdit;
    @BindView(R.id.search_store_edit_bg)
    LinearLayout searchStoreEditBg;
    @BindView(R.id.search_store_bg)
    RelativeLayout searchStoreBg;
    @BindView(R.id.tablayout)
    TabLayout tablayout;
    private MainFragmentAdapter mainAdapter;
    private StoreSearchFragment store;
    private ShopSearchFragment shop;
    public  String data="";
    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
        data=getIntent().getStringExtra("data");
        searchStoreEdit.setText(data);
        searchStoreEdit.setSelection(searchStoreEdit.getText().length());
        searchStoreEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    if(TextUtils.isEmpty(searchStoreEdit.getText().toString().trim())){
                        showToast(getString(R.string.search_content_is_not_null));
                        return false;
                    }
                    SoftInputUtils.closedSoftInput(StoreShopSearchResultActivity.this);
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_shop_search_result);
        ButterKnife.bind(this);
        initComponent();
        initData();
    }

    @OnClick({R.id.back_bt,R.id.search_tv_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;

            case R.id.search_tv_bt:
                if(TextUtils.isEmpty(searchStoreEdit.getText().toString().trim())){
                    showToast(getString(R.string.search_content_is_not_null));
                }
                SoftInputUtils.closedSoftInput(StoreShopSearchResultActivity.this);

                break;
        }
    }
   public void initData(){
       setupTabIcons();
   }

    //初始化底部选项文字以及图片
    public void setupTabIcons() {
        if (store_shop_ar == null || store_shop_ar.length == 0 || tablayout == null) {
            return;
        }
        int size = store_shop_ar.length;

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(store = new StoreSearchFragment());
        fragmentList.add(shop = new ShopSearchFragment());
        viewpager.setAdapter(mainAdapter = new MainFragmentAdapter(this, getSupportFragmentManager(), fragmentList));
        viewpager.setNoScroll(true);

        for (int i = 0; i < size; i++) {
            tablayout.addTab(tablayout.newTab().setCustomView(getTabView(i)));
        }
        //监听被选中子菜单
        tablayout.addOnTabSelectedListener(this);

    }

    /**
     * 获取子菜单
     *
     * @param position 当前位置
     * @return 得到子菜单选项
     */
    //得到底部子菜单选项
    public View getTabView(int position) {
        View tabView = View.inflate(this, R.layout.item_main_tab_title, null);
        TextView txt_title = tabView.findViewById(R.id.txt_title);
        txt_title.setText(store_shop_ar[position]);

        if (position == 0) {
            txt_title.setTextColor(getResources().getColor(R.color.colorAccent));
            txt_title.getPaint().setFakeBoldText(true);
            //  img_title.setImageResource(imgsMenu[position * 2]);
        } else {
            txt_title.setTextColor(getResources().getColor(R.color.default_black));
            txt_title.getPaint().setFakeBoldText(false);
            //  img_title.setImageResource(imgsMenu[position * 2 + 1]);
        }
        return tabView;
    }

    /**
     * 当被选中变化子菜单内容
     *
     * @param tab 子菜单组件
     */
    //选中时tab字体颜色和icon
    private void changeTabSelect(TabLayout.Tab tab) {
        //如果不等于空
        assert tab.getCustomView() != null;

        TextView txt_title = tab.getCustomView().findViewById(R.id.txt_title);
        int position = tab.getPosition();
        txt_title.setTextColor(getResources().getColor(R.color.colorAccent));
        txt_title.getPaint().setFakeBoldText(true);
//        img_title.setImageResource(main_active_ar[position * 2]);
        viewpager.setCurrentItem(tablayout.getSelectedTabPosition(), false);
//        AnimationSet animationSet = new AnimationSet(true);
//        ScaleAnimation scaleAnimation = new ScaleAnimation(1,1.2f,1,1.2f,
//                Animation.RELATIVE_TO_SELF,1.0f,Animation.RELATIVE_TO_SELF,1.0f);
//        //3秒完成动画
//        scaleAnimation.setDuration(100);
//        //将AlphaAnimation这个已经设置好的动画添加到 AnimationSet中
//        animationSet.addAnimation(scaleAnimation);
//        //启动动画
//        img_title .startAnimation(animationSet);
    }

    /**
     * 没有被选中变化子菜单内容
     *
     * @param tab 子菜单组件
     */
    //默认tab字体颜色和icon
    private void changeTabNormal(TabLayout.Tab tab) {
        assert tab.getCustomView() != null;

        TextView txt_title = tab.getCustomView().findViewById(R.id.txt_title);
        int position = tab.getPosition();
        txt_title.setTextColor(getResources().getColor(R.color.default_black));
        txt_title.getPaint().setFakeBoldText(false);
//        img_title.setImageResource(imgsMenu[position * 2 + 1]);

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        changeTabSelect(tab);
        viewpager.setCurrentItem(tab.getPosition(), false);
    }

    //监听子菜单没被选中
    @Override
    public void onTabUnselected(TabLayout.Tab tab) {
        changeTabNormal(tab);
    }


    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
